package myjava.test.xml.reader;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public abstract class ContextXml implements DealContext {
	
	private InputStream inputStream;
	
	private Document document;
	
	private Element parent;
	
	/**
	 * 根据inputStream初始化ContentXml
	 * @param path 文档的名字
	 */
	public ContextXml(String path) {
		this.inputStream = this.getClass().getResourceAsStream(path);
		this.init();
	}
	
	/**
	 * 确定初始化方式
	 * @param path 文档路径
	 * @param isRootPath 若文档在类的同级目录下，即src目录下，则isRootPath设置为false，否则设置为true
	 */
	public ContextXml(String path, boolean isRootPath) {
		if(isRootPath){
			this.init(path);
		}else{
			this.inputStream = this.getClass().getResourceAsStream(path);
			this.init();
		}
	}
	
	public void init(){
		SAXReader reader = new SAXReader();
		try {
			this.document = reader.read(this.inputStream);
			this.parent = document.getRootElement();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public void init(String path){
		SAXReader reader = new SAXReader();
		try {
			this.document = reader.read(path);
			this.parent = document.getRootElement();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public Element getElementById(String element, String value){
		return getElement(element, "id", value);
	}
	
	public Element getElement(String element, String id, String value){
		return this.getElement(this.parent, element, id, value);
	}
	
	@SuppressWarnings("unchecked")
	public Element getElement(Element ele, String element, String id, String value){
		//element 刚好是根节点
		if(this.parent.getName().equals(element))
			return ele;
		//递归遍历节点，找到节点element
		List<Element> elements = ele.elements();
		if(elements==null)
			return null;
		for(Element e : elements){
			if(e.getName().equals(element)&&e.attributeValue(id).equals(value))
				return e;
			else{
				Element isElement = getElement(e, element, id, value);
				if(isElement!=null)
					return isElement;
			}
		}
		return null;
	}

	@Override
	public String getValue(String element, String id, String value) {
		return this.getElement(element, id, value).getTextTrim();
	}

	@Override
	public String getValue(Element element) {
		return element.getTextTrim();
	}

	@Override
	public String getValue(String element, String value) {
		return this.getElementById(element, value).getTextTrim();
	}

	@Override
	public String getSonValue(String element, String sonElement, String id, String value) {
		return this.getElement(element, id, value).elementTextTrim(sonElement);
	}

	@Override
	public String getSonValue(Element element, String sonElement) {
		return element.elementText(sonElement);
	}
	
	@Override
	public String getSonValue(String element, String sonElement, String vlaue) {
//		System.out.println(this.getElementById(element, vlaue).attributeValue("id"));	
		return this.getElementById(element, vlaue).element(sonElement).getTextTrim();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Element> getSonElement(String parentElement, String id, String value) {
		return this.getElement(parentElement, id, value).elements();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Element> getSonElement(Element parentElement) {
		return parentElement.elements();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Element> getSonElement(String parentElement, String value) {
		return this.getElementById(parentElement, value).elements();
	}

	@Override
	public Element getParentElement(String sonElement, String id, String value) {
		return this.getElement(sonElement, id, value).getParent();
	}

	@Override
	public Element getParentElement(Element sonElement) {
		return sonElement.getParent();
	}
	public Element getParent(){
		return this.parent;
	}
	
}
